package com.water.wl.newwater;


import android.util.Log;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.water.wl.newwater.adapter.NewsAdapter;
import com.water.wl.newwater.api.ApiClient;
import com.water.wl.newwater.base.BaseListFragment;
import com.water.wl.newwater.base.ListBaseAdapter;
import com.water.wl.newwater.bean.News;
import com.water.wl.newwater.bean.NewsList;
import com.water.wl.newwater.util.XmlUtils;

import java.io.InputStream;
import java.util.List;

/**
 * Created by wds1993225 on 2016/3/10.
 */
public class MeTab extends BaseListFragment<News> {



    @Override
    protected NewsAdapter getListAdapter() {
        return new NewsAdapter();
    }
    @Override
    protected NewsList parseList(InputStream is) throws Exception {
        NewsList list = null;
        try {
            list = XmlUtils.toBean(NewsList.class, is);
        } catch (NullPointerException e) {
            list = new NewsList();
        }
        Log.i("新闻解析",String.valueOf(list));
        return list;
    }

    @Override
    protected void sendRequestData() {
        getNewsList(1,0,mHandler);
    }
    @Override
    protected void executeOnLoadDataSuccess(List<News> data) {
        if (mCatalog == NewsList.CATALOG_WEEK
                || mCatalog == NewsList.CATALOG_MONTH) {
            if (mState == STATE_REFRESH)
                mAdapter.clear();
            mAdapter.addData(data);
            mState = STATE_NOMORE;
            mAdapter.setState(ListBaseAdapter.STATE_NO_MORE);
            return;
        }
        super.executeOnLoadDataSuccess(data);
    }
    public static void getNewsList(int catalog, int page,
                                   AsyncHttpResponseHandler handler) {

        Log.i("新闻","启动");
        RequestParams params = new RequestParams();
        params.put("catalog", catalog);
        params.put("pageIndex", page);
        params.put("pageSize", 20);
        if (catalog == NewsList.CATALOG_WEEK) {
            params.put("show", "week");
        } else if (catalog == NewsList.CATALOG_MONTH) {
            params.put("show", "month");
        }
        ApiClient.get("action/api/news_list", params, handler);
    }

}
